<?php

namespace App\Repository\XhApi;

use App\Models\Xh\AddressModel;
use App\Repository\BaseRepository;

class AddressRepository extends BaseRepository
{
    public $model;
    public $log;

    public function __construct()
    {
        $this->model = new AddressModel();
        parent::__construct();
    }

    public function index($params, $page = 1, $limit = 30)
    {
        $where = [];
        $where['where']['member_id'] = $params['member_id'];
        $field = $this->model->getTableField();
        $orderBy = ['id' => 'desc'];
        return $this->unifiedGetPage($where, $page, $limit, $field, $orderBy);
    }

//删除指定id的内容
    public function del($id, $memberId = 0)
    {
        $this->model->where(['id' => $id, 'member_id' => $memberId])->delete();
        return $this->returnMsg(200, "删除操作完成");
    }

//新增内容
    public function add($postData, $memberId = 0)
    {
        return $this->addAndEdit($postData, [
            'name' => '收货地址必填写',
            'phone' => '收货电话必填写',
            'province' => '请选择省',
            'city' => '请选择城市',
            'area' => '请选择区县',
            'address' => '具体路道/村庄/门牌等请填写'
        ], $memberId);
    }

//修改内容
    public function edit($postData, $memberId = 0)
    {
        return $this->addAndEdit($postData, [
            'id' => '数据编号异常',
            'name' => '收货地址必填写',
            'phone' => '收货电话必填写',
            'province' => '请选择省',
            'city' => '请选择城市',
            'area' => '请选择区县',
            'address' => '具体路道/村庄/门牌等请填写'
        ], $memberId);
    }

    public function addAndEdit($request, $rulesFieldMsg = [], $memberId = 0)
    {
        if (empty($request)) {
            return $this->returnMsg(2, '操作数据不能为空');
        }
        if (empty($memberId)) {
            return $this->returnMsg(2, '请指明会员编号');
        }
        $id = 0;
        $pk = 'id';
        $optionalFields = $this->model->getTableField();
        $mustField = [];
        if (!empty($rulesFieldMsg) && is_array($rulesFieldMsg)) {
            $mustField = array_keys($rulesFieldMsg);
        }
        $updateData = $this->getRealData($request, $mustField, $optionalFields, $rulesFieldMsg);

        if (!empty($updateData) && is_string($updateData)) {
            //返回错误参数
            return $this->returnMsg(
                2,
                $updateData
            );
        }
        if (isset($updateData[$pk]) && $updateData[$pk] > 0) {
            $id = $updateData[$pk];
        }
        if ($id > 0) {
//            修改内容
            $where = [$pk => $id, 'member_id' => $memberId];
            $haveData = $this->model->where($where)->count($pk);
            if (!$haveData) {
                return $this->returnMsg(101, '未查询到修改的数据' . $this->getLastSql());
            }
            unset($updateData[$pk]);
            $res = $this->model->where($where)->update($updateData);
            if (false !== $res) {
                if (isset($updateData['default']) && $updateData['default'] == 1) {
                    $this->model->where(['member_id' => $memberId])->update(['default' => 0]);
                    $this->model->where([$pk => $id, 'member_id' => $memberId])->update(['default' => 1]);
                }
                return $this->returnMsg(200, '修改成功');
            } else {
                return $this->returnMsg(20, '修改错误');
            }
        } else {
//            新增
            $updateData['member_id'] = $memberId;
            $id = $this->model->insertGetId($updateData);
            if ($id > 0) {
                if (isset($updateData['default']) && $updateData['default'] == 1) {
                    $this->model->where(['member_id' => $memberId])->update(['default' => 0]);
                    $this->model->where([$pk => $id, 'member_id' => $memberId])->update(['default' => 1]);
                }
                $updateData[$pk] = $id;
                return $this->returnMsg(200, '添加成功', $updateData);
            }
            return $this->returnMsg(3, '内容保存异常');
        }
    }
}
